package com.dq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dq.commonutils.dto.OrderCourseVo;
import com.dq.commonutils.pojo.DqException;
import com.dq.commonutils.pojo.ResultCode;
import com.dq.eduservice.entity.EduCourse;
import com.dq.eduservice.entity.EduCourseDescription;
import com.dq.eduservice.entity.EduTeacher;
import com.dq.eduservice.service.*;
import com.dq.eduservice.mapper.EduCourseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.eduservice.vo.BasicCourseInfoVo;
import com.dq.eduservice.vo.CoursePubulishVo;
import com.dq.eduservice.vo.CourseVo;
import com.dq.eduservice.vo.FrontCourseVo;
import com.dq.eduservice.vo.param.CourseParam;
import com.dq.eduservice.vo.param.PageParam;
import com.dq.eduservice.vo.param.front.CourseFrontParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程(EduCourse)表服务实现类
 *
 * @author Dq
 * @since 2022-07-22 19:48:50
 */
@Service
public class EduCourseServiceImpl  extends ServiceImpl<EduCourseMapper, EduCourse>  implements EduCourseService {
    @Autowired
    private EduCourseMapper courseMapper;

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Override
    public String addCourse(CourseParam courseParam) {
        /**
         * 1 两张表
         * 2. 分别插入course  courseInfo
         * 3. 由于是一对一 所以设置id主键一致
         */
        EduCourse eduCourse = new EduCourse();
        System.out.println("before"+eduCourse.getId());
        BeanUtils.copyProperties(courseParam,eduCourse);
        //插入课程表
        int infectNum = courseMapper.insert(eduCourse);
        if (infectNum<=0){
            throw new DqException(ResultCode.ERROR.getCode(),"添加课程信息失败");
        }
        //获取eduCourseId
        String eduCourseId = eduCourse.getId();
        System.out.println("after"+eduCourse.getId());

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseParam.getDescription());
        eduCourseDescription.setId(eduCourseId);
        //插入描述数据表
        infectNum = courseDescriptionService.addCourseDescription(eduCourseDescription);
        if (infectNum<=0){
            throw new DqException(ResultCode.ERROR.getCode(),"添加课程信息失败");
        }

        return eduCourseId;
    }

    @Override
    public CourseParam getCourseInfo(String courseId) {
        /**
         * 1. 查询课程信息
         * 2. 查询课程描述
         */
        //查询课程基本信息
        EduCourse eduCourse = courseMapper.selectById(courseId);
        CourseParam courseParam = new CourseParam();
        BeanUtils.copyProperties(eduCourse,courseParam);
        //查询课程简介
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        courseParam.setDescription(eduCourseDescription.getDescription());
        return courseParam;
    }

    @Override
    public void updateCourseInfo(CourseParam courseParam) {
        /**
         * 1. 修改课程表
         * 2. 修改描述表
         */
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseParam,eduCourse);
        int update = courseMapper.updateById(eduCourse);
        if (update==0){
            throw new DqException(ResultCode.ERROR.getCode(),"修改失败");
        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseParam.getId());
        eduCourseDescription.setDescription(courseParam.getDescription());
        boolean update1 = courseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePubulishVo getPublishCourseInfo(String courseId) {
        /**
         * 1. 获取课程id
         * 2. 自写sql左外连接语法
         * 3. 查出来封装为Vo对象
         * 4. 返回统一结果集
         */
        CoursePubulishVo coursePubulishInfo = courseMapper.getCoursePubulishInfo(courseId);
        return coursePubulishInfo;
    }

    @Override
    public Map<String,Object> getCourseBasicInfoByPageCondition(PageParam pageParam) {
        /**
         * 1. 设置分页
         * 2. 判断条件进行if拼接
         * 3. 封装Vo结果变为List集合
         * 4. 结果统一返回
         */
        HashMap<String, Object> resultMap = new HashMap<>();
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<EduCourse> eduCoursePage = new Page<>(pageParam.getPage(), pageParam.getPageSize());

        if (!StringUtils.isEmpty(pageParam.getTitle())){
            eduCourseLambdaQueryWrapper.like(EduCourse::getTitle,pageParam.getTitle());
        }
        if (!StringUtils.isEmpty(pageParam.getStatus())){
            eduCourseLambdaQueryWrapper.eq(EduCourse::getStatus,pageParam.getStatus());
        }
        //查询  id  标题  发布状态 价格  课程数
        eduCourseLambdaQueryWrapper.select(
                EduCourse::getId,
                EduCourse::getTitle,
                EduCourse::getStatus,
                EduCourse::getPrice,
                EduCourse::getLessonNum,
                EduCourse::getViewCount,
                EduCourse::getBuyCount
        );
        //获取最后的结果
        Page<EduCourse> selectPage = courseMapper.selectPage(eduCoursePage, eduCourseLambdaQueryWrapper);
        List<EduCourse> records = selectPage.getRecords();
        List<BasicCourseInfoVo> basicCourseInfoVos = copyList(records);
        resultMap.put("courseBasicInfo",basicCourseInfoVos);
        resultMap.put("total",selectPage.getTotal());
        resultMap.put("currentPage",selectPage.getCurrent());
        resultMap.put("pageSize",selectPage.getSize());
        return resultMap;
    }

    @Override
    public void deleteCourseAllInfo(String courseId) {
        /**
         * 1. 根据课程id 删除小节 TODO 删除所有视频
         * 2. 根据课程id 删除章节
         * 3. 根据课程id 删除课程描述
         *
         * 4. 根据课程id 删除课程
         */
        // 1
        videoService.deleteVideoByCourseId(courseId);
        chapterService.deleteChapterByCourseId(courseId);
        courseDescriptionService.deleteCourseDescriptionByCourseId(courseId);
        int deleteNum = courseMapper.deleteById(courseId);
        if (deleteNum==0){
            throw new DqException(ResultCode.ERROR.getCode(),"删除失败");
        }

    }

    @Override
    public Map<String, Object> getCourseFrontList(CourseFrontParam courseFrontParam) {
        Page<EduCourse> page = new Page<>(courseFrontParam.getCurrentPage(), courseFrontParam.getPageSize());
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.eq(EduCourse::getStatus,"Normal");
        //条件过滤
        if(!StringUtils.isEmpty(courseFrontParam.getSubjectParentId())){ //一级分类
            eduCourseLambdaQueryWrapper.eq(EduCourse::getSubjectParentId,courseFrontParam.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontParam.getSubjectId())){  //二级分类
            eduCourseLambdaQueryWrapper.eq(EduCourse::getSubjectId,courseFrontParam.getSubjectId());
        }
        //排序  关注度  最新   价格
        if (!StringUtils.isEmpty(courseFrontParam.getBuyCountSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getBuyCount);
        }
        if (!StringUtils.isEmpty(courseFrontParam.getGmtCreateSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getGmtModified);
        }
        if (!StringUtils.isEmpty(courseFrontParam.getPriceSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getPrice);
        }
        Page<EduCourse> coursePage = courseMapper.selectPage(page, eduCourseLambdaQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("items",coursePage.getRecords());
        map.put("current",coursePage.getCurrent());
        map.put("pages",coursePage.getPages());
        map.put("size",coursePage.getSize());
        map.put("total",coursePage.getTotal());
        map.put("hasNext",coursePage.hasNext());
        map.put("hasPrevious",coursePage.hasPrevious());
        return map;
    }

    /**
     * 前端获取最终课程最详细信息
     * @param courseId
     * @return
     */
    @Override
    public FrontCourseVo getFinalCourseDetailInfo(String courseId) {
        FrontCourseVo courseVo=courseMapper.getFinalCourseDetailInfo(courseId);
        return courseVo;
    }





    //远程调用环节

    /**
     * @from service-order
     */

    /**
     * 根据课程id返回课程信息 dto
     * @param courseId
     * @return
     */
    @Override
    public OrderCourseVo getOrderCourseInfo(String courseId) {
        CoursePubulishVo coursePubulishInfo = courseMapper.getCoursePubulishInfo(courseId);
        OrderCourseVo orderCourseVo = new OrderCourseVo();
        BeanUtils.copyProperties(coursePubulishInfo,orderCourseVo);
        return orderCourseVo;
    }

    /**
     * List集合封装
     * @param eduCourseList
     * @return
     */
    public static List<BasicCourseInfoVo> copyList(List<EduCourse> eduCourseList){
        List<BasicCourseInfoVo> voArrayList = new ArrayList<>();
        for (EduCourse eduCourse : eduCourseList) {
            voArrayList.add(copy(eduCourse));
        }
        return voArrayList;
    }

    /**
     * 单独封装
     * @param eduCourse
     * @return
     */
    public static BasicCourseInfoVo copy(EduCourse eduCourse){
        BasicCourseInfoVo basicCourseInfoVo = new BasicCourseInfoVo();
        BeanUtils.copyProperties(eduCourse,basicCourseInfoVo);
        return basicCourseInfoVo;
    }


}